import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { message } from 'antd';
import { authService } from '../lib/authService';

interface ProtectedRouteProps {
  children: React.ReactElement;
}

const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ children }) => {
  const location = useLocation();
  
  // 调试信息
  const isAuthenticated = authService.isAuthenticated();
  const manager = authService.getManager();
  
  console.log('🔒 ProtectedRoute 检查:', {
    path: location.pathname,
    isAuthenticated,
    manager,
    localStorage: localStorage.getItem('currentManager')
  });

  if (!isAuthenticated) {
    console.log('❌ 认证失败，重定向到登录页');
    // 提示信息可以根据需要保留或移除
    message.warning('请先登录以访问此页面');
    // 重定向到登录页，并记录原始意图的页面
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return children;
};

export default ProtectedRoute; 